package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.mapper.OrderSettingMapper;
import com.itheima.pojo.OrderSetting;
import com.itheima.service.OrderSettingService;
import com.itheima.utils.DateUtil;
import org.apache.dubbo.config.annotation.Service;

import java.util.*;

/**
 * company: www.abc.com
 * Author: 29746
 * Create Data: 2020/11/15
 */
@Service(timeout = 30000)
public class OrderSettingServiceImpl extends ServiceImpl<OrderSettingMapper, OrderSetting> implements OrderSettingService {
    @Override
    public boolean importFile(List<String[]> data) {
        List<OrderSetting> list = new ArrayList<>();
        for (String[] row : data) {
            String orderDate=row[0];
            System.out.println(orderDate);
            int number= Integer.parseInt(row[1]);
            OrderSetting orderSetting= findByDate(orderDate);
            if (orderSetting==null){
                orderSetting = new OrderSetting();
            }
            orderSetting.setOrderDate(DateUtil.parseDate(orderDate,"yyyy/MM/dd"));
            orderSetting.setNumber(number);
            list.add(orderSetting);
        }
        this.saveOrUpdateBatch(list);
        return true;
    }

    @Override
    public OrderSetting findByDate(String orderDate) {
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("order_date",orderDate);
        return this.getOne(queryWrapper);
    }

    @Override
    public Map selectByYearMonth(int year, int month) {
        Map map = new HashMap<>();
        QueryWrapper<OrderSetting> queryWrapper = new QueryWrapper<>();
        String yearMonth=year+"-"+(month<10?"0":"")+month;
        queryWrapper.eq("date_format(order_date,'%Y-%m')",yearMonth);
        List<OrderSetting> orderSettingList = this.list(queryWrapper);
        for (OrderSetting orderSetting : orderSettingList) {
            //显示在前台所以要转成字符串
            String orderDate = DateUtil.formatDate(orderSetting.getOrderDate());
            map.put(orderDate,orderSetting);
        }
        return map;
    }

    @Override
    public boolean settingDate(String day, int num) {
        OrderSetting orderSetting = findByDate(day);
        if (orderSetting==null){
            orderSetting=new OrderSetting();
            orderSetting.setOrderDate(DateUtil.parseDate(day));
        }
        orderSetting.setNumber(num);

        return this.saveOrUpdate(orderSetting);
    }


}
